<?php
class UsersService extends Service{

    
    /**
     * @param int id
     * <pre>
     * {"type":"integer"}
     * </pre>
     * @return void
     */
    public static function delete($id) {
        if (self::_invoke()) return self::_output();
        $user = Doctrine::getTable('User')->find($id);
        $user->delete();
    }
    

    /**
     * @param object $data
     * <pre>
     * {
     *   "type":"object",
     *   "properties": {
     *     "id":{"type":"string"},
     *     "name":{"type":"string","minLength":5},
     *     "password":{"type":"string","minLength":5},
     *     "generalRoles":{"type":"array"},
     *     "projectRoles":{
     *       "type":"array",
     *       "items":{
     *         "type":"object",
     *         "properties":{
     *           "project_id":{"type":"integer"},
     *           "role_id":{"type":"integer"}
     *         }
     *       }
     *     }
     *   }
     * }
     * </pre>
     * @return void
     */
    public static function edit($data) {
        if (self::_invoke()) return self::_output();
        $user = Doctrine::getTable('User')->find($data->id);
        $user->name = $data->name;
        $user->password = $data->password;
        UserTable::relateGeneralRoles($user->id, $data->generalRoles);
        UserTable::relateProjectRoles($user->id, $data->projectRoles);
        $user->save();
    }

    /**
     * @param object $data
     * <pre>
     * {
     *   "type":"object",
     *   "properties": {
     *     "name":{"type":"string","minLength":5},
     *     "password":{"type":"string","minLength":5}
     *   }
     * }
     * </pre>
     * @return int
     */
    public static function insert($data) {
        if (self::_invoke()) return self::_output();
        $conn = Doctrine_Manager::connection();
        $conn->beginTransaction();

        $user = new User;
        $user->name = $data->name;
        $user->password = $data->password;
        $user->save();

        UserTable::relateGeneralRoles($user->id, $data->generalRoles);
        UserTable::relateProjectRoles($user->id, $data->projectRoles);

        $conn->commit();
        return $user->id;
    }
    
    /**
     * @param int id
     * @return object
     * {
     *   "type":"object",
     *   "properties": {
     *     "id":{"type":"string"},
     *     "name":{"type":"string","minLength":5},
     *     "password":{"type":"string","minLength":5}
     *   }
     * }
     */
    public static function get($id) {
        $user = UserTable::findWithRoles($id);
        return $user;
    }

    /**
     * @param int id
     * @return object
     * {
     *   "type":"object",
     *   "properties": {
     *     "id":{"type":"string"},
     *     "name":{"type":"string","minLength":5},
     *     "password":{"type":"string","minLength":5}
     *   }
     * }
     */
    public static function getByName($name) {
        $user = UserTable::findWithRolesAndPermissions(null, $name);
        return $user;
    }

    /**
     * @param int id
     * @return object
     * <pre>
     * {
     *   "type":"object",
     *   "properties": {
     *     "id":{"type":"string"},
     *     "name":{"type":"string","minLength":5},
     *     "password":{"type":"string","minLength":5}
     *   }
     * }
     * </pre>
     */
    public static function getAll() {
        $users = UserTable::findAllWithRoles();
        return $users;
    }


}